<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-tabs>
          <el-tab-pane label="角色管理">
            <el-row>
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog = true"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table border :data="list">
              <el-table-column label="序号" width="120" type="index">
              </el-table-column>
              <el-table-column label="角色" width="240" prop="name">
              </el-table-column>
              <el-table-column label="描述" prop="description">
              </el-table-column>
              <el-table-column label="操作">
                <!--作用域插槽 row??? -->
                <template slot-scope="{ row }">
                  <el-button type="success" @click="assignPerm(row.id)">
                    分配权限
                  </el-button>
                  <el-button type="primary" @click="editRole(row.id)"
                    >编辑</el-button
                  >
                  <el-button type="danger" @click="deleteRole(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <el-pagination
                layout="prev, pager, next"
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                @current-change="changePage"
              >
              </el-pagination>
            </el-row>
          </el-tab-pane>
          <!-- 公司信息 -->
          <el-tab-pane label="公司信息"></el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 放置一个弹层组件 -->
      <el-dialog title="编辑部门" :visible.sync="showDialog">
        <el-form ref="roleForm" :model="roleForm" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="btnOk">确定</el-button>
          <el-button>取消</el-button>
        </div>
      </el-dialog>

      <!-- 分配权限的弹层 -->
      <el-dialog
        title="分配权限"
        :visible="showPermDialog"
        @close="btnPermCancel"
      >
        <el-tree
          ref="permTree"
          :data="permData"
          :props="defaultProps"
          :show-checkbox="true"
          :check-strictly="true"
          :default-expand-all="true"
          :default-checked-keys="selectCheck"
          node-key="id"
        />
        <!-- 确定 取消 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK"
              >确定</el-button
            >
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { tranListToTreeData } from "@/utils/index";
import {
  deleteRole,
  getRoleList,
  updateRole,
  getRoleDetail,
  addRole,
  assignPrem
} from "@/api/setting";
import { getPermissionList } from "@/api/permission";
export default {
  data() {
    return {
      permData: [], // 权限列表数据-- 树形数据
      showPermDialog: false, // 分配权限控制
      defaultProps:{
        label: 'name'
      },
      selectCheck:[], // 选中的权限节点
      roleId: '',  // 角色id
      list: [], // 角色列表
      page: {
        page: 1, // 当前页面
        pagesize: 10, //每页条数
        total: 0, // 总数量
      },
      showDialog: false,
      roleForm: {}, // 接收编辑或新增的表单数据
      rules: {
        name: [{ required: true, $message: "角色名称能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getRoleList();
  },
  methods: {
    async assignPerm(id) {
      // 点击分配权限 传递角色id
      // 获取权限列表数据
      this.permData = tranListToTreeData(await getPermissionList(), "0");
      // 保存当前点击的角色id
      this.roleId = id;
      // 获取选中的权限
      const {permIds} = await getRoleDetail(id);
      this.selectCheck = permIds;

      this.showPermDialog = true;
    },
    async btnPermOK() {
      // 分配权限确定
      await assignPrem({
        permIds: this.$refs.permTree.getCheckedKeys(),
        id: this.roleId,
      });
      this.$message.success("分配权限成功");
      this.showPermDialog = false;
    },
    btnPermCancel() {
      // 点击分配权限弹层关闭
      this.selectCheck = [];
      this.showPermDialog = false;
    },
    // 获取角色列表
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page);
      // console.log(total);
      this.page.total = total;
      this.list = rows;
    },
    changePage(newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage;
      // 重新渲染
      this.getRoleList();
    },

    // 删除角色
    async deleteRole(id) {
      try {
        await this.$confirm("确定要删除吗?");
        await deleteRole(id);
        this.getRoleList();
        this.$message.success("删除角色成功!");
      } catch (error) {
        console.log(error);
      }
    },

    // 编辑角色
    async editRole(id) {
      this.roleForm = await getRoleDetail(id);
      this.showDialog = true;
    },
    // 定义弹出框的确定按钮
    async btnOk() {
      try {
        // 验证 rules
        await this.$refs.roleForm.validate();
        if (this.roleForm.id) {
          // 编辑
          await updateRole(this.roleForm);
        } else {
          // 新增
          await addRole(this.roleForm);
          // console.log(this.roleForm)
        }
        // 重新获取数据 并渲染页面
        this.getRoleList();
        this.$message.success("操作成功");
        this.showDialog = false;
        // 操作完成后 重置弹框
        this.roleForm = {};
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>
<style scoped></style>